package com.ruoyi.orders.service.impl;

import com.ruoyi.orders.domain.Orders;
import com.ruoyi.orders.domain.OrdersName;
import com.ruoyi.orders.mapper.OrdersMapper;
import com.ruoyi.orders.service.IOrdersService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl implements IOrdersService
{
    private static final String CACHE_NAME = "orders";

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询订单（带缓存）
     */
    @Override
    @Cacheable(value = CACHE_NAME, key = "#orderId")
    public Orders selectOrdersByOrderId(Long orderId)
    {
        return ordersMapper.selectOrdersByOrderId(orderId);
    }

    /**
     * 查询订单列表（带缓存）
     */
    @Override
    @Cacheable(value = CACHE_NAME, key = "#orders.cacheKey()")
    public List<Orders> selectOrdersList(Orders orders)
    {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 查询带名称的订单列表（带缓存）
     */
    @Override
    @Cacheable(value = CACHE_NAME, key = "#orders.nameCacheKey()")
    public List<OrdersName> selectOrdersListName(Orders orders)
    {
        List<Orders> ordersList = ordersMapper.selectOrdersList(orders);
        List<OrdersName> result = new ArrayList<>();

        for (Orders order : ordersList) {
            OrdersName ordersName = new OrdersName();
            ordersName.setUserName(order.getUserName());
            ordersName.setOrderId(order.getOrderId());
            ordersName.setOrderDate(order.getOrderDate());
            ordersName.setTotalAmount(order.getTotalAmount());
            ordersName.setStatus(order.getStatus());
            ordersName.setPaymentMethod(order.getPaymentMethod());
            ordersName.setShippingAddress(order.getShippingAddress());
            result.add(ordersName);
        }

        return result;
    }

    /**
     * 新增订单（同步缓存）
     */
    @Override
    @CacheEvict(value = CACHE_NAME, allEntries = true)
    public int insertOrders(Orders orders)
    {
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改订单（同步缓存）
     */
    @Override
    @CacheEvict(value = CACHE_NAME, key = "#orders.orderId")
    public int updateOrders(Orders orders)
    {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单（同步缓存）
     */
    @Override
    @CacheEvict(value = CACHE_NAME, allEntries = true)
    public int deleteOrdersByOrderIds(Long[] orderIds)
    {
        return ordersMapper.deleteOrdersByOrderIds(orderIds);
    }

    /**
     * 删除订单（同步缓存）
     */
    @Override
    @CacheEvict(value = CACHE_NAME, key = "#orderId")
    public int deleteOrdersByOrderId(Long orderId)
    {
        return ordersMapper.deleteOrdersByOrderId(orderId);
    }
}